package com.fullstack.controller;

import com.databases.dao.UserMapper;
import com.fullstack.bean.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/**
 * 对数据库 用户进行的操作
 */
public class UserController {

    /**
     * 增加用户信息
     * @param request
     */
    public void adduserinformation(HttpServletRequest request){
        User user = new User();

        //注入值
        user.setUsername(request.getParameter("form[username]"));
        user.setPassword(request.getParameter("form[password]"));
        user.setName(request.getParameter("form[name]"));
        user.setBirthday(request.getParameter("form[birthday]"));
        user.setSex(request.getParameter("form[sex]"));
        user.setPhone(request.getParameter("form[phone]"));
        user.setAddress(request.getParameter("form[address]"));
        user.setEmail(request.getParameter("form[email]"));

        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象

            UserMapper mapper = session.getMapper(UserMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            mapper.adduser(user);


            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public String checkupdate(HttpServletRequest request){

        String password_from_sql = null;
        String inf = "no";
        String password_from_form = request.getParameter("form[password]");

        try {
            //执行Mybatis 存取数据库
            String resource = "SqlMapConfig.xml";           //绑定主体.xml

            //对SqlMapConfig.xml读取并解析
            InputStream inputStream = Resources.getResourceAsStream(resource);

            //获取会话工厂SqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //SqlSession主要用于对数据库的操作
            SqlSession session =  sessionFactory.openSession();     //获取数据库执行对象

            UserMapper mapper = session.getMapper(UserMapper.class);//接口名.class 获取对象   反射机制，通过接口，执行.xml里sql语句

            password_from_sql = mapper.checkupdate(request.getParameter("form[name]"));

            System.out.println(password_from_sql);
            session.commit();     //增删改需要执行事件 查询不用
            session.close();      //关闭
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        if (password_from_sql.equals(password_from_form)){
            inf = "yes";
        }
        else {
            inf= "no";
        }
        System.out.println(password_from_sql);

        return inf;

    }
}
